home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / utils1 / fip09.arj / SOURCE.ZIP / GLOBAL.CPP < prev    next >
C/C++ Source or Header  |  1993-11-17  |  7KB  |  228 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module global.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/c/fips/source/cpp/RCS/global.cpp 0.9.1.1 1993/11/17 17:51:06 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include <stdarg.h>
  32. #include <conio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "global.h"
  36.  
  37. #define CTRL_C 3
  38.  
  39. global_vars global;
  40.  
  41. /* ----------------------------------------------------------------------- */
  42. /* Initialization of global variables                                      */
  43. /* ----------------------------------------------------------------------- */
  44.  
  45. global_vars::global_vars (void)
  46. {
  47.     quiet_mode = false;
  48.     test_mode = false;
  49.     verbose_mode = true;
  50.     debug_mode = false;
  51.  
  52.     override_multiple_boot = false;
  53.     override_bootable_flag = false;
  54.     override_rootdir_entries = false;
  55.     override_large_fat = false;
  56.     override_small_fat = false;
  57.     override_media_descriptor = false;
  58.  
  59.     drive_number_cmdline = 0;
  60.     partition_number_cmdline = 0;
  61.     new_start_cylinder_cmdline = 0;
  62.  
  63.     backup_root = true;
  64.     ask_if_backup = true;
  65. }
  66.  
  67. global_vars::~global_vars (void)
  68. {
  69.     if (debug_mode) fclose (debugfile);
  70. }
  71.  
  72. void exit_function (void)
  73. {
  74.     printx ("\nBye!\n");
  75. }
  76.  
  77. void global_vars::open_debugfile (int argc,char *argv[])
  78. {
  79.     if ((debugfile = fopen ("fipsinfo.dbg","wt")) == NULL)
  80.     {
  81.         global.debug_mode = false;
  82.         warning ("Can't open debugfile");
  83.     }
  84.     else
  85.     {
  86.         fprintf (debugfile,"FIPS Version 0.9 beta\n\n");
  87.         fprintf (debugfile,"The command was: ");
  88.         while (argc--) fprintf (debugfile,argc ? "%s " : "%s", *argv++);
  89.         fprintf (debugfile,"\n\nTranscript of session:\n");
  90.     }
  91. }
  92.  
  93. /* ----------------------------------------------------------------------- */
  94. /* Replacement for printf - prints to screen and debugfile                 */
  95. /* ----------------------------------------------------------------------- */
  96.  
  97. void printx (char *fmt,...)
  98. {
  99.     va_list ap;
  100.     va_start (ap,fmt);
  101.     vprintf (fmt,ap);
  102.     if (global.debug_mode) vfprintf (global.debugfile,fmt,ap);
  103.     va_end (ap);
  104. }
  105.  
  106. /* ----------------------------------------------------------------------- */
  107. /* Replacement for getch - exit when CTRL-C is pressed                     */
  108. /* ----------------------------------------------------------------------- */
  109.  
  110. int getx (void)
  111. {
  112.     int character = getch();
  113.     if (character == CTRL_C)
  114.     {
  115.         printx ("\n");
  116.         exit (0);
  117.     }
  118.     return (character);
  119. }
  120.  
  121. /* ----------------------------------------------------------------------- */
  122. /* Copyright notice and version number                                     */
  123. /* ----------------------------------------------------------------------- */
  124.  
  125. void notice (void)
  126. {
  127.     printx ("\nFIPS version 0.9 beta, Copyright (C) 1993 Arno Schaefer\n");
  128.     printx ("Please read the file README.1ST\n");
  129.     printx ("FIPS comes with ABSOLUTELY NO WARRANTY, see file COPYING for details\n");
  130.     printx ("This is free software, and you are welcome to redistribute it\n");
  131.     printx ("under certain conditions; again see file COPYING for details.\n");
  132. }
  133.  
  134. /* ----------------------------------------------------------------------- */
  135. /* Hexdump binary data into a file                                         */
  136. /* ----------------------------------------------------------------------- */
  137.  
  138. void hexwrite (byte *buffer,int number,FILE *file)
  139. {
  140.     for (int i=0;i<number;i++)
  141.     {
  142.         fprintf (file,"%02X ",*(buffer+i));
  143.         if ((i+1)%16 == 0) fprintf (file,"\n");
  144.         else if ((i+1)%8 == 0) fprintf (file,"- ");
  145.     }
  146.     fprintf (file,"\n");
  147. }
  148.  
  149. /* ----------------------------------------------------------------------- */
  150. /* Error Handling                                                          */
  151. /* ----------------------------------------------------------------------- */
  152.  
  153. static void print_verbose_message (char *message)
  154. {
  155.     char line[256];
  156.     int length = 0;
  157.     FILE *error_msg_file;
  158.  
  159.     fprintf (stderr,"\n");
  160.     if (global.debug_mode) fprintf (global.debugfile,"\n");
  161.  
  162.     if ((error_msg_file = fopen ("errors.txt","rt")) == NULL)
  163.     {
  164.         fprintf (stderr,"File ERRORS.TXT not found - no verbose messages available\n");
  165.         if (global.debug_mode) fprintf (global.debugfile,"File ERRORS.TXT not found - no verbose messages available\n");
  166.         global.verbose_mode = false;
  167.         return;
  168.     }
  169.  
  170.     while (message[length] != 0 && message[length] != ':') length++;
  171.  
  172.     fgets (line,255,error_msg_file);
  173.     while (strncmp(message,line,length)) if (fgets (line,255,error_msg_file) == NULL) return;
  174.     fgets (line,255,error_msg_file);
  175.     while (!strncmp("  ",line,2))
  176.     {
  177.         fprintf (stderr,"%s",line+2);
  178.         if (global.debug_mode) fprintf (global.debugfile,"%s",line+2);
  179.         if (fgets (line,255,error_msg_file) == NULL) return;
  180.     }
  181.     fclose (error_msg_file);
  182. }
  183.  
  184. void error (char *message,...)
  185. {
  186.     va_list ap;
  187.  
  188.     fprintf (stderr,"\nError: ");
  189.     if (global.debug_mode) fprintf (global.debugfile,"\nError: ");
  190.  
  191.     va_start (ap,message);
  192.     vfprintf (stderr,message,ap);
  193.     if (global.debug_mode) vfprintf (global.debugfile,message,ap);
  194.     va_end (ap);
  195.  
  196.     fprintf (stderr,"\n");
  197.     if (global.debug_mode) fprintf (global.debugfile,"\n");
  198.  
  199.     if (global.verbose_mode) print_verbose_message (message);
  200.  
  201.     exit (-1);
  202. }
  203.  
  204. void warning (char *message,...)
  205. {
  206.     va_list ap;
  207.     char *p;
  208.  
  209.     fprintf (stderr,"\nWarning: ");
  210.     if (global.debug_mode) fprintf (global.debugfile,"\nWarning: ");
  211.  
  212.     va_start (ap,message);
  213.     vfprintf (stderr,message,ap);
  214.     if (global.debug_mode) vfprintf (global.debugfile,message,ap);
  215.     va_end (ap);
  216.  
  217.     fprintf (stderr,"\n");
  218.     if (global.debug_mode) fprintf (global.debugfile,"\n");
  219.  
  220.     if (global.verbose_mode) print_verbose_message (message);
  221.  
  222.     fprintf (stderr,"\nPress any key\n");
  223.     if (global.debug_mode) fprintf (global.debugfile,"\nPress any key\n");
  224.  
  225.     getx();
  226. }
  227.  
  228.